package com.chen.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chen.entity.Book;
import com.chen.service.IBookService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CacheConfig(cacheNames = "book")//缓存名称
public class BookController {
    @Autowired
    IBookService iBookService;

    @Resource
    CacheManager cacheManager;

    @GetMapping("/getAll")
    @Cacheable(key = "targetClass+'userList'")
    public List<Book> getList() {
        return iBookService.list();
    }

    @GetMapping("/byId/{id}")
    //condition为 条件是奇数就进去
    @Cacheable(key = "#id", condition = "#id%2==0")
    public Book byId(@PathVariable Integer id) {
        return iBookService.getById(id);
    }

    @GetMapping("/byIds/{id}")
    @Cacheable(key = "#book.id")
    public Book byId(Book book) {
        return iBookService.getById(book.getId());
    }

    @CacheEvict(key = "#id")
    @DeleteMapping("/del/{id}")
    public String deleteBook(@PathVariable Integer id) {
        System.out.println("从数据库删除一本书，同时删除key为" + id + "的缓存");
        return "删除成功";
    }

    @CacheEvict(key = "#id", allEntries = true) //删除所有的缓存
    @DeleteMapping("/delAll/{id}")
    public String delAll(@PathVariable Integer id) { //根据id号删除一本书, 同时删除缓存
        System.out.println("从数据库删除一本书，同时删除key为" + id + "的缓存");
//        bookService.removeById(id);
        return "删除成功";
    }


    @CachePut(key = "#book.id")//修改缓存
    public Book updateBook(@RequestBody Book book) { //修改一本书
        System.out.println("从数据库修改一本书，同时修改缓存，key为" + book.getId());
        iBookService.saveOrUpdate(book);
        return book;
    }

    //定义缓存的键为拼接的参数,值是方法的返回值
    @Cacheable(key="(#condition.name!=null?#condition.name:'')+(#condition.category!=null?#condition.category:'')+(#condition.author!=null?#condition.author:'')")
    @PostMapping("/codition")
    public List<Book>searchBook(@RequestBody Book condition){
        System.out.println("条件查询，并添加到缓存，key为搜索条件的组合");
        System.out.println("condition:"+condition);
        LambdaQueryWrapper<Book>queryWrapper=new LambdaQueryWrapper<>();
        if(StringUtils.hasText(condition.getName())){
            queryWrapper.like(Book::getName,condition.getName());
        }
        if(StringUtils.hasText(condition.getCategory())){
            queryWrapper.like(Book::getCategory,condition.getCategory());
        }
        if(StringUtils.hasText(condition.getAuthor())){
            queryWrapper.like(Book::getAuthor,condition.getAuthor());
        }
        return iBookService.list(queryWrapper);
    }

    @Cacheable(value="books",keyGenerator = "bookSearchGenerator")  //定义缓存的键为自定义的键生成策略,值是方法的返回值
    @PostMapping("/conditionTwo")
    public List<Book> searchBooks(@RequestBody Book condition){ //查找一本书
        System.out.println("条件查询,并添加到缓存，key为搜索条件的组合");
        System.out.println("condition:"+condition); LambdaQueryWrapper<Book> queryWrapper= new LambdaQueryWrapper<>();
        if (StringUtils.hasText(condition.getName())) {
            queryWrapper.like(Book::getName,condition.getName());
        }
        if (StringUtils.hasText(condition.getCategory())) {
            queryWrapper.like(Book::getCategory,condition.getCategory());
        }
        if (StringUtils.hasText(condition.getAuthor())) {
            queryWrapper.like(Book::getAuthor,condition.getAuthor());
        }
        return iBookService.list(queryWrapper);
    }
}
